package com.zisu.wcsopcua.dispatch.controller;

import com.zisu.wcsopcua.business.entity.TWcsTask;
import com.zisu.wcsopcua.business.entity.TWcsTaskLog;
import com.zisu.wcsopcua.business.service.ITWcsTaskLogService;
import com.zisu.wcsopcua.business.service.ITWcsTaskService;
import com.zisu.wcsopcua.common.utils.CommonFunction;
import com.zisu.wcsopcua.dispatch.constant.QGSConstant;
import com.zisu.wcsopcua.dispatch.entity.WarehousingEntity;
import com.zisu.wcsopcua.dispatch.service.DDJService;
import com.zisu.wcsopcua.enums.ddj.Plc2WcsVarEnum;
import com.zisu.wcsopcua.enums.ddj.Wcs2PlcVarEnum;
import com.zisu.wcsopcua.opcua.controller.R;
import com.zisu.wcsopcua.opcua.selfunion.Enum.PLCType;
import com.zisu.wcsopcua.opcua.selfunion.UAService;
import org.eclipse.milo.opcua.stack.core.types.builtin.DataValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * @Author: 蔡翔
 * @Date: 2019/10/12 15:15
 * @Version 1.0
 */
@RestController
@RequestMapping("/wcs")
public class WcsController {
    @Autowired
    DDJService service;

    @Autowired
    UAService uaService;

    @Autowired
    ITWcsTaskService taskService;

    @Autowired
    ITWcsTaskLogService logService;

    //todo
    // 1.新增了两个变量   枚举类中那两个变量的地址新增上。
    // 2.把application.yml 里面的 url改成实际环境的。
    @PostMapping("/sendTask")
    public R sendTask(@RequestBody WarehousingEntity warehousingEntity) {
        //1.先录到 任务表、日志表
        //①
        TWcsTask tWcsTask = new TWcsTask();
        tWcsTask.setCreateTime(LocalDateTime.now());
        tWcsTask.setValid(1);
        tWcsTask.setUpdateTime(LocalDateTime.now());
        tWcsTask.setCode(warehousingEntity.getTaskNo().toString());
        //wms 发给我的都是正在执行，在排队中的任务留在wms中先别给我
        tWcsTask.setStatus(1);
        //int all = taskMapper.insertSelective(tWcsTask);
        taskService.save(tWcsTask);

        //②
        TWcsTaskLog log = new TWcsTaskLog();
        log.setValid(1);
        log.setCreateTime(LocalDateTime.now());
        log.setContent("任务编号："+warehousingEntity.getTaskNo().toString());
        log.setSendStatus("否");
        log.setReceiveStatus("是");
        log.setSource("wms");
        log.setUpdateTime(LocalDateTime.now());
        //logService.create(log);
        logService.save(log);

        //发起堆垛机任务
        Integer integer = service.warehousingTask(warehousingEntity);


        //2.修改 任务表、日志表中的记录
        TWcsTask tWcsTaskUpdate = new TWcsTask();
        tWcsTaskUpdate.setStatus(integer);
        tWcsTaskUpdate.setId(tWcsTask.getId());
        taskService.update(tWcsTaskUpdate);

        TWcsTaskLog logUpdate = new TWcsTaskLog();
        logUpdate.setId(log.getId());
        logUpdate.setSendStatus("是");
        logService.update(logUpdate);


        if(integer!=1){
            return R.error(integer.toString());
        }else {
            return R.ok(integer.toString());
        }
    }


    //test for ddj read and write
    @PostMapping("/testForReadForPlc2Wcs")
    public void testForReadForPlc2Wcs() throws Exception {
        for(Plc2WcsVarEnum var:Plc2WcsVarEnum.values()){
            System.out.println(
                    var.getName() +" valueIs : "+
                            Boolean.valueOf(getValue(var))+ "type : "+
                            CommonFunction.judgeVarType(
                                    getValues(var).getValue().getValue()
                            )
            );
        }
    }
    @PostMapping("/testForReadForWcs2Plc")
    public void testForReadForWcs2Plc() throws Exception {
        //boolean
        for(Wcs2PlcVarEnum var:Wcs2PlcVarEnum.values()){
            System.out.println(
                    var.getName() +" valueIs : "+
                    Boolean.valueOf(getValue(var))+ "type : "+
                    CommonFunction.judgeVarType(
                            getValues(var).getValue().getValue()
                    )
            );
        }
    }
    @PostMapping("/testForWrite")
    public void testForWrite() throws Exception {
        Boolean STB = setValue(Wcs2PlcVarEnum.STB,Wcs2PlcVarEnum.STB.getType(), 1);
        System.out.println("STB : "+STB);
        Boolean ACK = setValue(Wcs2PlcVarEnum.ACK,Wcs2PlcVarEnum.ACK.getType(), 1);
        System.out.println("ACK : "+ACK);

        Boolean lane = setValue(Wcs2PlcVarEnum.LaneNum,Wcs2PlcVarEnum.LaneNum.getType(),1);
        System.out.println("lane : "+lane);
        Boolean pickRow = setValue(Wcs2PlcVarEnum.PickRow,Wcs2PlcVarEnum.PickRow.getType(), 1);
        System.out.println("pickRow : "+pickRow);
        Boolean pickBay = setValue(Wcs2PlcVarEnum.PickBay,Wcs2PlcVarEnum.PickBay.getType(), 1);
        System.out.println("pickBay : "+pickBay);
        Boolean pickLevel = setValue(Wcs2PlcVarEnum.PickLevel,Wcs2PlcVarEnum.PickLevel.getType(), 1);
        System.out.println("pickLevel : "+pickLevel);
        Boolean sendBay = setValue(Wcs2PlcVarEnum.SendBay,Wcs2PlcVarEnum.SendBay.getType(), 1);
        System.out.println("sendBay : "+sendBay);
        Boolean sendRow = setValue(Wcs2PlcVarEnum.SendRow,Wcs2PlcVarEnum.SendRow.getType(), 1);
        System.out.println("sendRow : "+sendRow);
        Boolean sendLevel = setValue(Wcs2PlcVarEnum.SendLevel,Wcs2PlcVarEnum.SendLevel.getType(), 1);
        System.out.println("sendLevel : "+sendLevel);
        Boolean taskNo = setValue(Wcs2PlcVarEnum.TaskNo,Wcs2PlcVarEnum.TaskNo.getType(), 1);
        System.out.println("taskNo : "+taskNo);
        Boolean taskType = setValue(Wcs2PlcVarEnum.TaskType,Wcs2PlcVarEnum.TaskType.getType(), 1);
        System.out.println("taskType : "+taskType);
    }

    private String getValue(Plc2WcsVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName).getValue().getValue().toString();
    }

    private String getValue(Wcs2PlcVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName).getValue().getValue().toString();
    }
    private DataValue getValues(Wcs2PlcVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName);
    }
    private DataValue getValues(Plc2WcsVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName);
    }

    private Boolean setValue(Wcs2PlcVarEnum var,String dataType,Object newValue) throws Exception {
        return uaService.setValue(
                var.getNamespace(),
                var.getIdentity(),
                PLCType.valueOf(dataType).convertType(newValue),
                QGSConstant.plcName);
    }
    private Boolean setValue(Plc2WcsVarEnum var,String dataType,Object newValue) throws Exception {
        return uaService.setValue(
                var.getNamespace(),
                var.getIdentity(),
                PLCType.valueOf(dataType).convertType(newValue),
                QGSConstant.plcName
        );
    }
}
